package cn.wolfcode.luowowo.search.service.impl;

import cn.wolfcode.luowowo.search.query.IndexSearchQuery;
import cn.wolfcode.luowowo.search.repository.UserInfoTemplateRepository;
import cn.wolfcode.luowowo.search.service.IUserInfoTemplateSearchService;
import cn.wolfcode.luowowo.search.service.SearchQueryService;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.SearchResultMapper;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.data.elasticsearch.core.aggregation.impl.AggregatedPageImpl;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @author 猫教授
 * @date 2019/8/17 18:46
 */
@Service
public class UserInfoTemplateSearchServiceImpl implements IUserInfoTemplateSearchService {
    //操作se搜索引擎的共有三个操作方式,分别con底层到上层
    //第一种,是repository接口
    @Autowired
    private UserInfoTemplateRepository repository;
    //第二种是template
    @Autowired
    private ElasticsearchTemplate template;
    //第三种是最底层的
    @Autowired
    private TransportClient client;
    @Autowired
    private SearchQueryService searchQueryService;

    @Override
    public void save(UserInfoTemplate userInfoTemplate) {
        repository.save(userInfoTemplate);
    }

    @Override
    public List<UserInfoTemplate> findByDestName(String keyword) {
        return repository.findByDestName(keyword);
    }

    @Override
    public AggregatedPage<UserInfoTemplate> selectByKeyword(IndexSearchQuery qo,String...fieldName) {
        //<span style='color:red;'></span>
        return searchQueryService.selectByKeyword(qo,"<span style='color:red;'>","</span>",UserInfoTemplate.class,UserInfoTemplate.INDEX_NAME,UserInfoTemplate.TYPE_NAME,fieldName);
    }


}
